//
//  MenuView.m
//  JieFangShop2.0
//
//  Created by Apple on 15/4/16.
//  Copyright (c) 2015年 yuanlin. All rights reserved.
//

#import "MenuView.h"

#define kCellHeight 40
@interface MenuView()
{
    NSArray *_array;
    NSInteger _numOfMenu;
    NSMutableArray *_titles;
    NSMutableArray *_indicators;
    NSInteger _currentSelectedIndex;
    UIImageView *arrowView;
    BOOL isMore;
    UIColor *_menuColor;
}
@end
@implementation MenuView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
    
    }
    return self;
}
-(MenuView *)initWithArray:(NSArray *)array
{
    if (self = [super init]) {
     self.backgroundColor=[UIColor whiteColor];
        _menuWidth=kMainScreen_Width*0.85;
        _menuColor=RED_COLOR;
     self.frame = CGRectMake(0, 0, kMainScreen_Width, kNAV_HEIGHT);
     
     _array = array;
     _numOfMenu = _array.count;
     CGFloat separatorLineInterval = _menuWidth / _numOfMenu;
     CGFloat textLayerInterval = _menuWidth /(_numOfMenu * 2);
      _titles = [[NSMutableArray alloc] initWithCapacity:_numOfMenu];
      _indicators = [[NSMutableArray alloc] initWithCapacity:_numOfMenu];
        
       for (int i = 0; i < _numOfMenu; i ++) {
            CGPoint position = CGPointMake((i * 2 + 1)* textLayerInterval, self.frame.size.height / 2);
            CATextLayer *title = nil;
            title = [self createTextLayerWithNSString:_menutitles[i] Color:[UIColor blackColor] Position:position];
            [self.layer addSublayer:title];
            [_titles addObject:title];
           
           if (i==0 ||i == (_numOfMenu - 1)) {
               CGPoint separatorPosition = CGPointMake((i + 1)* separatorLineInterval, self.frame.size.height / 2);
               CAShapeLayer *separator = [self createSeparatorLineWithColor:[UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:243.0/255.0 alpha:1.0] Position:separatorPosition];
               [self.layer addSublayer:separator];
           }
           
        }
        [self animateTitle:_titles[0] Show:NO Complete:^{
        }];
        UIGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMenu:)];
        [self addGestureRecognizer:tapGesture];
        
        arrowView=[[UIImageView alloc]initWithFrame:CGRectMake(kMainScreen_Width*0.88, 10, 22, 20)];
        [arrowView setImage:[UIImage imageNamed:@"arrowdown"]];
        [self addSubview:arrowView];
    }
    return self;
}

#pragma mark 处理菜单点击事件
- (void)tapMenu:(UITapGestureRecognizer *)paramSender
{
    CGPoint touchPoint = [paramSender locationInView:self];
    //获取tapIndex
    NSInteger tapIndex = touchPoint.x /(_menuWidth / _numOfMenu);
    
    _currentSelectedIndex=tapIndex;
    if (_currentSelectedIndex>=_numOfMenu) {
        isMore=!isMore;
        if (isMore) {
            [arrowView setImage:[UIImage imageNamed:@"arrowup"]];
        }else{
            [arrowView setImage:[UIImage imageNamed:@"arrowdown"]];
        }
        if ([_delegate respondsToSelector:@selector(showAllMenuWith:)]) {
            [_delegate showAllMenuWith:isMore];
        }
    }else{
        //设置非点击按钮状态
        for (NSInteger i = 0; i < _numOfMenu; i ++) {
            if (i == tapIndex) {
                [self animateTitle:_titles[i] Show:NO Complete:^{
                }];
            }else{
                [self animateTitle:_titles[i] Show:YES Complete:^{
                }];
            }
        }
    }
    if ([_delegate respondsToSelector:@selector(clickMenuAt:)]) {
        [_delegate clickMenuAt:tapIndex];
    }
}

- (CATextLayer *)createTextLayerWithNSString:(NSString *)string Color:(UIColor *)color Position:(CGPoint)point
{
    CGSize size = [self calculateTitleSizeWithString:string];
    CATextLayer *layer = [CATextLayer new];
    CGFloat sizeWidth = (size.width < (_menuWidth / _numOfMenu)- 25) ? size.width : (_menuWidth / _numOfMenu - 25);
    layer.bounds = CGRectMake(0, 0, sizeWidth, size.height);
    layer.string = string;
    layer.fontSize = 15.0;
    layer.alignmentMode = kCAAlignmentCenter;
    layer.foregroundColor = color.CGColor;
    
    layer.contentsScale = [[UIScreen mainScreen] scale];
    layer.position = point;
    
    return layer;
}

- (CGSize)calculateTitleSizeWithString:(NSString *)string
{
    CGFloat fontSize = 15.0;
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:fontSize]};
    CGSize size = [string boundingRectWithSize:CGSizeMake(280, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
    return size;
}
- (CAShapeLayer *)createSeparatorLineWithColor:(UIColor *)color Position:(CGPoint)point
{
    CAShapeLayer *layer = [CAShapeLayer new];
    
    UIBezierPath *path = [UIBezierPath new];
    [path moveToPoint:CGPointMake(160, 0)];
    [path addLineToPoint:CGPointMake(160, 20)];
    
    layer.path = path.CGPath;
    layer.lineWidth = 1.0;
    layer.strokeColor = color.CGColor;
    
    CGPathRef bound = CGPathCreateCopyByStrokingPath(layer.path, nil, layer.lineWidth, kCGLineCapButt, kCGLineJoinMiter, layer.miterLimit);
    layer.bounds = CGPathGetBoundingBox(bound);
    layer.position = point;
    
    return layer;
}
- (void)animateTitle:(CATextLayer *)title Show:(BOOL)show Complete:(void(^)())complete
{
    if (show) {
        [title setForegroundColor:[UIColor blackColor].CGColor];
    }
    else {
        [title setForegroundColor:_menuColor.CGColor];
    }
    CGSize size = [self calculateTitleSizeWithString:title.string];
    CGFloat sizeWidth = (size.width < (_menuWidth / _numOfMenu)- 25) ? size.width : (_menuWidth / _numOfMenu - 25);
    title.bounds = CGRectMake(0, 0, sizeWidth, size.height);
    complete();
}
@end




